package org.icue.billeos;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.media.Buffer;
import javax.media.CannotRealizeException;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Controller;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSourceException;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.control.FormatControl;
import javax.media.control.FrameGrabbingControl;
import javax.media.control.PortControl;
import javax.media.format.VideoFormat;
import javax.media.protocol.DataSource;
import javax.media.util.BufferToImage;

import com.sun.media.amovie.VisualComponent;


public class Capture {
	
	private MediaLocator ml;
	private CaptureDeviceInfo di;
	private Player player;

	public Capture() 
	{
		
		di = CaptureDeviceManager.getDevice("vfw://0"); 
	    ml = new MediaLocator("vfw:Microsoft WDM Image Capture (Win32):0");

	    DataSource ds;
		try {
			ds = Manager.createDataSource(ml);
			player = Manager.createRealizedPlayer(ds);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    	//System.out.println("Before Manager");
		Component component = player.getVisualComponent();
		FrameGrabbingControl fgc = (FrameGrabbingControl) 
		player.getControl("javax.media.control.FrameGrabbingControl");
		//VideoFormat videoFormat = (VideoFormat) ((FormatControl) (player.getControl("javax.media.control.FrameGrabbingControl"))).getFormat(); 

		//c.setSize(640, 480);
		component.setSize(640, 480);
    	//System.out.println("Before port control");
    	PortControl formatControl = (PortControl)player.getControl("javax.media.control.PortControl");
    	player.start();
        //System.out.println("Sleeping");
        try {
			Thread.currentThread();
			Thread.sleep(8000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println("Extracting image");
	}
	
	/**
	 * Retrieve the image from the current video
	 * 
	 * @return PathName of Picture Created
	 */
	public String captureImage()
	{
		String pathName = null;
		Calendar currentCal;
		
		currentCal = Calendar.getInstance();
		
		
		
		
		pathName = "C://";
		pathName += "img";
		pathName += currentCal.getTime().getTime();
		pathName += ".jpg";
		
	        // Grab a frame 
		FrameGrabbingControl fgc = (FrameGrabbingControl) 
		player.getControl("javax.media.control.FrameGrabbingControl");

		Buffer buf = fgc.grabFrame();
		// Convert it to an image 
		BufferToImage btoi = new BufferToImage((VideoFormat)buf.getFormat()); 
		Image img = btoi.createImage(buf); 
		/*Image img = null;
		try {
			img = ImageIO.read(new File("C:/abc.jpg"));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
		
		BufferedImage bi = new BufferedImage(640, 480, BufferedImage.TYPE_3BYTE_BGR);
		Graphics2D g2 = bi.createGraphics();
        g2.drawImage(img, 0, 0, null);
        g2.dispose();
		try {
			System.out.println("Picture " + pathName + " taken");
			File fileTemp = new File(pathName);
			ImageIO.write(bi, "jpg", fileTemp);
			System.out.println("Picture " + pathName + " saved");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// show the image 
		//imgpanel.setImage(img); 
	   
		// save image 
		//saveJPG(img,chemin); 
		return pathName;
	}
	
	/**
	 *  Dispose the object before quiting
	 */
	public void dispose()
	{
		player.stop();
		player.deallocate();
		player.close();
	}
}
